﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Pout_Porri.Objects.Interface;
using Pout_Porri.Utils;

namespace Pout_Porri.Objects
{
    class HedgeHog : Enemy
    {
        private Animation hedgeHogStand, hedgeHogWalk;
        private double elapsedTime;
        private float frameRate;

        public HedgeHog(Texture2D image, Vector2 position, Vector2 velocity) : base(image, position, velocity)
        {
            this.texture = image;
            this.Mass = 0.5f;
            setGrounded(true);
            this.frameRate = 10;
            this.DefaultVelocityX = velocity.X;
            //isAnimated = true;
        }

        public override void configAnimations()
        {
            hedgeHogStand = new Animation("hedgeHogstand", new Vector2(84, 84), 200, 8, 2, 1, Axis.X); 
            hedgeHogWalk = new Animation("hedgeHogwalk", new Vector2(84, 84), 200, 8, 2, 10, Axis.X);
            this.addAnimation(hedgeHogWalk);
            this.addAnimation(hedgeHogStand);

            this.spriteTexture = new Texture2D(Game1.graphics.GraphicsDevice, 84, 84);

            this.setCurrentAnimation("hedgeHogstand");
            currentState = EnemyStates.stand;
        }

        public override void update(GameTime gameTime)
        {
            checkMatrixCollisionInY();

            IA(MyLevel.getPlayer());
               
            if (currentState == EnemyStates.stand)
            {
                this.setCurrentAnimation("hedgeHogstand");
                
            }
            else if (currentState == EnemyStates.walking)
            {
                this.setCurrentAnimation("hedgeHogwalk");
            }

            Walk(gameTime);

            

            currentAnimationRect = this.AnimationRect(this.getCurrentAnimation());

            base.update(gameTime);
        }

        public void Walk(GameTime gameTime)
        {
            if (gameTime.TotalGameTime.TotalMilliseconds > elapsedTime + frameRate)
            {
                elapsedTime = gameTime.TotalGameTime.TotalMilliseconds;

                applyMovement(gameTime);
            }
            
            
        }

        public void IA(PhysicsGameEntity player)
        { 
            Game1.window.Title = "" + this.position.X + this.direction.X;
            
            if (player.position.X - 10 < this.position.X  && this.position.X < player.position.X + 10)
            {
                this.direction.X = 0;
                currentState = EnemyStates.stand;
            }
            
            else if (player.position.X + 10> this.position.X)
            {
                this.direction.X = 1;
                currentState = EnemyStates.walking;
            }

            else if (player.position.X - 10 < this.position.X)
            {
                this.direction.X = -1;
                currentState = EnemyStates.walking;
            }

            
        }

    }
}
